<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Cache;
use Log;
use App\Models\UserSummary;
use App\Models\UserCumulate;
use App\Models\ArticleSummary;
use App\Models\UserReadSummary;
use App\Models\UserShareSummary;
use App\Models\UpstreamMessageHourly;
use App\Models\UpstreamMessageSummary;

class HomeController extends Controller
{
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('微信公众号数据统计');
            //$content->description('');

            //$content->row(view('admin::dashboard.test'));

            $content->row(function (Row $row) {
                $userSummary = $this->getUserSummary();
                $row->column(3, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('累计关注用户', 'users', 'aqua', '/admin/users/totalCharts', $userSummary['cumulate_user']));
                });

                $row->column(3, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('昨天新增用户', 'users', 'aqua', '/admin/users/newaddCharts', $userSummary['new_user']));
                });

                $row->column(3, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('昨天取关用户', 'users', 'aqua', '/admin/users/cancelCharts', $userSummary['cancel_user']));
                });

                $row->column(3, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('昨天净增用户', 'users', 'aqua', '/admin/users/netaddCharts', $userSummary['new_user'] - $userSummary['cancel_user']));
                });
            });

            $content->row(function (Row $row) {
                $userSummary = $this->getUserSummary();
                $row->column(4, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('消息发送人数', 'commenting', 'yellow', '/admin/users', $userSummary['cumulate_user']));
                });

                $row->column(4, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('消息发送次数', 'commenting', 'yellow', '/admin/users', $userSummary['new_user']));
                });

                $row->column(4, function (Column $column) use ($userSummary) {
                    $column->append(new InfoBox('人均发送次数', 'commenting', 'yellow', '/admin/users', $userSummary['cancel_user']));
                });
            });

            $summary = $this->getArticleDateSummary();
            $headers = ['日期', '标题', '送达人数', '阅读次数', '分享人数'];
            $rows = [];
            foreach($summary as $row) {
                $rows[] = [
                    $row->ref_date,
                    $row->title,
                    $row->int_page_read_user,
                    $row->int_page_read_count,
                    $row->share_user,
                ];
            }
            $table = new Table($headers, $rows);

            $box = new Box('图文数据统计', $table->render());
            $content->row($box->render());

        });
    }

    protected function getUserSummary()
    {
        $date = date("Y-m-d", strtotime("-1 day"));
        $userCumulate = UserCumulate::where("ref_date", $date)->first();
        if(!$userCumulate) {
            return [
                'cumulate_user' => 0,
                'new_user' => 0,
                'cancel_user' => 0
            ];
        }
        $summary['cumulate_user'] = $userCumulate->cumulate_user;
        $userSummary = UserSummary::where("ref_date", $date)->get();
        $new_user = 0;
        $cancel_user = 0;
        foreach($userSummary as $row) {
            Log::info($row->newUser);
            $new_user += $row->new_user;
            $cancel_user += $row->cancel_user;
        }
        $summary['new_user'] = $new_user;
        $summary['cancel_user'] = $cancel_user;
        return $summary;
    }

    protected function getArticleDateSummary()
    {
        $rows = ArticleSummary::orderBy("id", 'desc')->limit(10)->get();
        return $rows;
    }
}
